package com.fire.control.system.support.util;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.fire.control.system.dto.SmsDto;

@Component
public class SmsUtil {
	private static final Logger logger = LoggerFactory.getLogger(SmsUtil.class);

	@Autowired
	public RestTemplate restTemplate;
	private String url = "http://yunpian.com/v1/sms/send.json";
	
	public boolean sendSmsCode(String phone, String code) {
		try {
			MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
			params.add("apikey", StaticValue.yupian_apikey);
			params.add("mobile", phone);
			params.add("text", "【小道科技】您的验证码是" + code + "。如非本人操作，请忽略本短信");
			HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params);
			Map<String, Object> map = restTemplate.postForEntity(url, httpEntity, Map.class).getBody();
			if (null == map || map.size() == 0)
				return false;
			int success = (Integer) map.get("code");
			if (success == 0) {
				return true;
			} else {
				logger.info("Send message :" + "mobile:" + phone + " Result:" + map);
			}
		} catch (Exception e) {
			logger.info(e.getMessage(), e);
		}
		return false;
	}
	
	public boolean sendSmsPwd(String phone, String loginName,String password) {
		try {
			MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
			params.add("apikey", StaticValue.yupian_apikey);
			params.add("mobile", phone);
			params.add("text", "【消防云平台】您已经在消防云平台注册成功，账号是"+loginName+"，初始密码为"+password+"，请您尽快登录平台修改密码，谢谢！");
			HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params);
			Map<String, Object> map = restTemplate.postForEntity(url, httpEntity, Map.class).getBody();
			if (null == map || map.size() == 0)
				return false;
			int success = (Integer) map.get("code");
			if (success == 0) {
				return true;
			} else {
				logger.info("Send message :" + "mobile:" + phone + " Result:" + map);
			}
		} catch (Exception e) {
			logger.info(e.getMessage(), e);
		}
		return false;
	}
	
	public SmsDto sendSmsWarning(String phone, String content) {
		SmsDto dto = new SmsDto();
		try {
			MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
			params.add("apikey", StaticValue.yupian_apikey);
			params.add("mobile", phone);
			params.add("text", "【消防云平台】您所负责/居住的"+content+"，请您及时处理！");
			HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params);
			Map<String, Object> map = restTemplate.postForEntity(url, httpEntity, Map.class).getBody();
			dto = JSONUtil.toBean(JSONUtil.toJson(map), SmsDto.class);
			dto.setMsg(JSONUtil.toJson(map));
			//logger.info("Send message :" + "mobile:" + phone + " Result:" + map);
		} catch (Exception e) {
			logger.info(e.getMessage(), e);
		}
		return dto;
	}

}
